package com.liit.console.controller.front;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liit.console.filter.WebContext;
import com.liit.module.entity.RetailArea;
import com.liit.module.frame.utils.Const;
import com.liit.module.frame.utils.ValidatorUtils;
import com.liit.module.frame.utils.page.Pagination;
import com.liit.module.service.DictService;
import com.liit.module.service.RetailAreaService;

/**
 * @author dingweibo
 * @Title: RetailAreaController.java
 * @Description: 城市或地区维护
 * @date 2015年12月21日
 */
@Controller
@RequestMapping(value = "/retailArea")
public class RetailAreaController {

	private Logger logger = Logger.getLogger(getClass());

	@Resource
	RetailAreaService retailAreaService;

	@Resource
	DictService dictService;

	/**
	 * 查询城市或地区
	 * 
	 * @param product
	 * @param pagination
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public String list(RetailArea retailArea, Pagination pagination) throws Exception {
		Map<String, Object> result = retailAreaService.listPage(pagination, retailArea);
		List<RetailArea> list = (List<RetailArea>) result.get("list");
		BeanUtils.copyProperties(result.get("page"), pagination);
		WebContext.setAttribute("list", list);
		WebContext.setAttribute(Const.PAGINATION, pagination);
		WebContext.setAttribute("retailRegion", dictService.selectByColumn(Const.RETAIL_REGION));
		return "/front/retailAreaList";
	}

	@RequestMapping(value = "/add")
	public String add(Integer id) throws Exception {
		WebContext.setAttribute("retailRegion", dictService.selectByColumn(Const.RETAIL_REGION));
		if (id != null) {
			RetailArea retailArea = retailAreaService.selectById(id);
			WebContext.setAttribute("retailArea", retailArea);
		}
		return "/front/retailAreaAdd";
	}

	@RequestMapping("/save")
	public String save(RetailArea retailArea) {
		try {
			Date now = new Date();
			String message = ValidatorUtils.validate(retailArea);
			if (StringUtils.isBlank(message)) {
				if (retailArea.getId() != 0) {
					retailArea.setUpdateTime(now);
					retailAreaService.updateById(retailArea);
				} else {
					retailArea.setCreateTime(now);
					retailArea.setUpdateTime(now);
					retailArea.setId(retailAreaService.insertAndReturnPK(retailArea));
				}
				WebContext.setAttribute(Const.MSG, "保存成功");
				WebContext.setAttribute("retailRegion", dictService.selectByColumn(Const.RETAIL_REGION));
			} else {
				WebContext.setAttribute(Const.MSG, message);
			}
		} catch (Exception e) {
			logger.error("保存失败", e);
			WebContext.setAttribute(Const.MSG, "保存失败");
		}
		return "/front/retailAreaAdd";
	}

	@RequestMapping(value = "/get/{region}")
	@ResponseBody
	public List<RetailArea> get(@PathVariable Integer region) throws Exception {
		return retailAreaService.selectAreaByRegion(region, Const.STATUS_ENABLED);
	}
}
